package com.zj.leetcode.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: algorithm
 * @description:
 *
 * 387. 字符串中的第一个唯一字符
 *
 * 和出现次数有关的，不要犹豫，hash
 * @author: Zhang Bo
 * @create: 2022-01-07 11:41
 **/
public class FirstUniqueCharacterInAString {
    public static void main(String[] args) {
        Solution solution = new FirstUniqueCharacterInAString().new Solution();
        String s = "leetcode";
        System.out.println(solution.firstUniqChar(s));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int firstUniqChar(String s) {
            Map<Character, Integer> map = new HashMap<>();

            for (int i = 0; i < s.length(); i++) {
                map.put(s.charAt(i),  map.getOrDefault(s.charAt(i), 0) + 1);
            }

            for (int i = 0; i < s.length(); i++) {
                if(map.get(s.charAt(i)) == 1){
                    return i;
                }
            }
            return -1;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
